#include <thirdparty/fist-log.h>
#include <core/task/task_interface.h>

#include "player_manager.h"
#include "player_conf.h"

CPlayerManager::CPlayerManager()
    : m_last_sync_info(0)
    , m_conf(new CPlayerConfParser())
    , m_timeout_count(0)
    , m_last_co(0)
{
    
}

bool CPlayerManager::Init()
{
    if (!m_conf->Load("../config/logic/player.xml")) {
        return false;
    }
    return true;
}
void CPlayerManager::Update()
{
    CMngPtr<ITaskManager> p_task_manager(this);
    if (service_id() == 1 && fist::get_now_ms_time() - m_last_co > 1000) {
        LOG_INFO("co tps:{}, timeout:{}, valid:{}", m_call_count, m_timeout_count, m_call_count - m_timeout_count);
        m_call_count = 0;
        m_timeout_count = 0;

        auto task = [this]()->cppcoro::task<> {
            co_await NewTask();
        };
        p_task_manager->PushTask(task);

        m_last_co = fist::get_now_ms_time();
    }
    if (service_id() == 1) {
        for (unsigned i = 1; i < cfg()->task_test_parallel; ++i) {
            auto task = [this]()->cppcoro::task<> {
                co_await NewTask();
            };
            p_task_manager->PushTask(task);
        }
    }
}
void CPlayerManager::Destroy()
{
    
}

const PlayerConf* CPlayerManager::cfg()
{
    return m_conf->cfg();
}

int CPlayerManager::rc_test(int a, int b)
{
    // LOG_INFO("call a:{}, b:{}", a, b);
    return a + b;
}

cppcoro::task<> CPlayerManager::NewTask()
{
    // LOG_INFO("start");
    CStubPtr<IPlayerManager> rmi(this, _any_lobby);
    int ret = co_await rmi->rc_test_co(1, 2);
    // LOG_INFO("return {}", ret);
    if (ret != 3) {
        m_timeout_count++;
    }
    m_call_count++;
    co_return;
}


